/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.stemaini.utility;

/**
 *
 * @author Administrator
 */
class HTMLParser 
{
        private String html;

        /**
         * Constructor, takes a string (containing HTML) as an argument
         *
         */
        public HTMLParser(String html) 
        {
            this.html = new String(html);
        }
         
        /**
         * Strips all instances of specified HTML tag.
         */
        public void stripTag(String tag) 
        {
            StringBuffer newhtml = new StringBuffer();
            int ch;
            boolean istag = false;

            for (int i = 1; i < this.html.length(); i++) 
            {
                ch = this.html.charAt(i);
                if ((istag == false) && (ch == '<')) 
                {
                    if (this.html.substring(i + 1, i + 1 + tag.length()).toLowerCase().equals(tag.toLowerCase()))
                        istag = true;

                    //String tag2 = tag.substring(0, tag.length()-1);
                    if ((this.html.charAt(i+1)  == '/') && (this.html.substring(i+2, i+2+tag.length())).toLowerCase().equals(tag.toLowerCase()) )  
                    {
                        istag = true;
                    }
                }

                if (istag == false)
                        newhtml.append((char) ch);
                if ((istag == true) && (ch == '>'))
                        istag = false;
            }
            this.html = newhtml.toString();
        }

        /**
         * Strips all instances of specified HTML tag and its contents.
         */
        public void stripTagContents(String tag) 
        {
                StringBuffer newhtml = new StringBuffer();
                int ch;
                boolean istag = false;
                for (int i = 0; i < this.html.length(); i++) 
                { //for sepanjang HTML

                    ch = this.html.charAt(i); //mengambil chart nya

                    if ((istag == false) && (ch == '<')) //jika menemukan < dan tidak dalam tag
                            if (this.html.substring(i + 1, i + 1 + tag.length()).toLowerCase().equals(tag.toLowerCase())) // substring(i+1, i+1+tag.length()) == tag
                                    istag = true;
                    if (istag == false)
                            newhtml.append((char) ch); //append
                    if ((istag == true) && (ch == '/') && //jika ditemukan /
                                    (this.html.substring(i+1, i+1+tag.length()).toLowerCase().equals(tag.toLowerCase())) && //dan substring(i+1, i+i+
                                    (this.html.charAt(ch + tag.length() + 1) == '>')) {
                            istag = false;
                            i += tag.length() + 1;
                    }
                }
                this.html = newhtml.toString();
        }

        /**
         * Strips all HTML tags, leaving only the text in between.
         *
         */
        public void stripTags() 
        {
            StringBuffer newhtml = new StringBuffer();
            int ch;
            boolean htmltag = false;
            for (int i = 0; i < html.length(); i++) 
            {
                ch = html.charAt(i);
                if ((htmltag == false) && (ch == '<'))
                        htmltag = true;
                if (htmltag == false)
                        newhtml.append((char) ch);
                if ((htmltag == true) && (ch == '>'))
                        htmltag = false;
            }
            this.html = newhtml.toString();
        }


        /**
         * Return the string contained within the first instance of the specified tag
         * Warning: Behavior is undefined for nested tags of the same type.
         */
        public String getTagContents(String tag) 
        {
                String t2 = "<";
                String t = t2.concat(tag.toLowerCase());

                int start = 0, end = this.html.length();
                // Find start

                for (int i = 0; i < this.html.length() - 1 - t.length(); i++) 
                {
                    if (this.html.substring(i, i + t.length()).toLowerCase().equals(t)) 
                    {
                        while (this.html.charAt(i) != '>') 
                        {
                                i++;
                        }
                        start = i + 1;
                        break;
                    }
                }

                t2 = "</";
                t = t2.concat(tag.toLowerCase());
                // Find end
                for (int i = start; i < this.html.length() - 1 - t.length(); i++) 
                {
                    if (this.html.substring(i, i + t.length()).toLowerCase().equals(t)) 
                    {
                        end = i;
                        break;
                    }
                }
                return this.html.substring(start, end);
        }

        /**
         * Returns a copy of the current HTML string.
         *
         */
        public String getString() 
        {
            return new String(this.html);
        }
}

